import observe from './observe';
import Dep from "./Dep";

export default function defineProperty(data, key, val) {
    const dep = new Dep();

    if (val === undefined) {
        val = data[key];
    }

    let childObj = observe(val);

    Object.defineProperty(data, key, {
        configurable: true,
        enumerable: true,
        get() {
            console.log(`试图获取${key}的值`);
            if (Dep.target) {
                dep.depend();
                if (childObj) {
                    childObj.dep.depend();
                }
            }

            return val;
        },
        set(newValue) {
            if(newValue === val) return ;

            val = newValue;
            childObj = observe(val);

            dep.notify();
        }
    })
};